package com.hightrast.service.converToClassMapper;

import com.hightrast.service.api.domain.bo.QueryShopifyProduct;
import com.hightrast.service.api.entity.RastProductImages;
import com.hightrast.service.api.entity.RastProductVariants;
import com.hightrast.service.api.entity.RastProducts;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;

import java.time.LocalDateTime;
import java.time.OffsetDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Mapper
public interface ProductMapper {
    ProductMapper INSTANCE = Mappers.getMapper(ProductMapper.class);

    @Mapping(source = "id", target = "id")
    @Mapping(source = "title", target = "title")
    @Mapping(source = "body_html", target = "bodyHtml")
    @Mapping(source = "vendor", target = "vendor")
    @Mapping(source = "product_type", target = "productType")
    @Mapping(source = "created_at", target = "createdAt")
    @Mapping(source = "updated_at", target = "updatedAt")
    @Mapping(source = "published_at", target = "publishedAt")
    @Mapping(source = "published_scope", target = "publishedScope")
    @Mapping(source = "tags", target = "tags")
    @Mapping(source = "status", target = "status")
    @Mapping(source = "variants", target = "variants")
    @Mapping(source = "images", target = "images")
    RastProducts queryShopifyToProduct(QueryShopifyProduct queryShopifyProduct);

    // 自定义方法：将 OffsetDateTime 转换为 LocalDateTime
    default LocalDateTime convertDateTime(OffsetDateTime offsetDateTime) {
        return offsetDateTime != null ? offsetDateTime.toLocalDateTime() : null;
    }

    default List<RastProductVariants> convertVariants(List<QueryShopifyProduct.ProductVariants> queryShopifyOrderProductItems) {
        if (queryShopifyOrderProductItems == null) {
            return null;
        }
        return queryShopifyOrderProductItems.stream()
                .map(ProductVariantMapper.INSTANCE::queryShopifyToProductVariant) // 自定义转换逻辑
                .collect(Collectors.toList());
    }
    default List<RastProductImages> convertImages(List<QueryShopifyProduct.ProductImages> queryShopifyOrderProductItems) {
        if (queryShopifyOrderProductItems == null) {
            return null;
        }
        return queryShopifyOrderProductItems.stream()
                .map(ProductImageMapper.INSTANCE::queryShopifyToProductImage) // 自定义转换逻辑
                .collect(Collectors.toList());
    }

    // 自定义转换方法：将 Boolean 转换为 Integer
    default Integer convertBoolean(Boolean value) {
        if(value == null){
            return null;
        }
        return value ? 1 : 0;
    }
}